
#########################
## Load and clean data ##
#########################

# load data
trdmat_excl <- read.csv("data/datasets/main_data.csv")
excl_agg <- read.csv("data/datasets/excl_agg.csv")
tars_covered <- read.csv("data/datasets/tars_covered.csv")

######################################
## Models imports 20122016-20182020 ##
######################################

## top 10
newdat <- data.frame(year = rep(c("20122016","20182020"), each = 2*nrow(trdmat_excl)), 
        hs10code = trdmat_excl$hs10code, impoth = trdmat_excl$imptop1020122016,
        imp = c(trdmat_excl$impchn20122016/5, trdmat_excl$impwor20122016/5, trdmat_excl$impchn20182020/3, trdmat_excl$impwor20182020/3)/1000,
        market = c(rep("China", nrow(trdmat_excl)), rep("Other", nrow(trdmat_excl)), rep("China", nrow(trdmat_excl)), rep("Other", nrow(trdmat_excl))),
        impothpos = c(trdmat_excl$imptop10pos20122016, trdmat_excl$imptop10pos20122016, trdmat_excl$imptop10pos20122016, trdmat_excl$imptop10pos20122016)
)
newdat$market <- factor(newdat$market, levels = c("Other","China"))

postrade <- trdmat_excl$hs10code[trdmat_excl$impchn20122016/5 > 0 | trdmat_excl$impwor20122016/5 > 0]
newdat2 <- newdat[newdat$hs10code %in% postrade,]
newdat2_top10 <- newdat2
mod <- lm(log(imp+1) ~ year*market*I(impothpos/8), data = newdat2);
mm_df <- data.frame(model.matrix(mod), imp = log(newdat2$imp+1), hs10code = newdat2$hs10code)
mm_df <- na.omit(mm_df)
colnames(mm_df) <- c("Intercept","year","market","impothpos",
  "year_market","year_impothpos","market_impothpos","year_market_impothpos",
  "imp","hs10code")
aggvars <- apply(mm_df[,colnames(mm_df)[2:9]], 2, as.numeric)
mm_df$hs10code_market <- paste(mm_df$hs10code,mm_df$market, sep = "")
agg <- aggregate(aggvars ~ hs10code_market, FUN = mean, data = mm_df)
colnames(agg) <- c("hs10code_market","year_mean","market_mean","impothpos_mean",
  "year_market_mean","year_impothpos_mean","market_impothpos_mean","year_market_impothpos_mean",
  "imp_mean")
mm_df <- join(mm_df, agg)
mm_df[,2:9] <- mm_df[,2:9] - mm_df[,12:19] 

mod1top10 <- lm(imp ~ year + year_market + year_impothpos + year_market_impothpos, data = mm_df)
mm_df$year_sector <- paste(newdat2$market, newdat2$year, substr(newdat2$hs10code,1,2),sep = "_")
mod2top10 <- lm(imp ~ year_impothpos + year_market_impothpos + year_sector, data= mm_df); 

## top 25
newdat <- data.frame(year = rep(c("20122016","20182020"), each = 2*nrow(trdmat_excl)), 
        hs10code = trdmat_excl$hs10code,
        imp = c(trdmat_excl$impchn20122016/5, trdmat_excl$impwor20122016/5, trdmat_excl$impchn20182020/3, trdmat_excl$impwor20182020/3)/1000,
        market = c(rep("China", nrow(trdmat_excl)), rep("Other", nrow(trdmat_excl)), rep("China", nrow(trdmat_excl)), rep("Other", nrow(trdmat_excl))),
        impothpos = c(trdmat_excl$imptop25pos20122016, trdmat_excl$imptop25pos20122016, trdmat_excl$imptop25pos20122016, trdmat_excl$imptop25pos20122016)
)
newdat$market <- factor(newdat$market, levels = c("Other","China"))

postrade <- trdmat_excl$hs10code[trdmat_excl$impchn20122016/5 > 0 | trdmat_excl$impwor20122016/5 > 0]
newdat2 <- newdat[newdat$hs10code %in% postrade,]
newdat2_top25 <- newdat2
mod <- lm(log(imp+1) ~ year*market*log(impothpos+1), data = newdat2);
mm_df <- data.frame(model.matrix(mod), imp = log(newdat2$imp+1), hs10code = newdat2$hs10code)
mm_df <- na.omit(mm_df)
colnames(mm_df) <- c("Intercept","year","market","impothpos",
  "year_market","year_impothpos","market_impothpos","year_market_impothpos",
  "imp","hs10code")
aggvars <- apply(mm_df[,colnames(mm_df)[2:9]], 2, as.numeric)
mm_df$hs10code_market <- paste(mm_df$hs10code,mm_df$market, sep = "")
agg <- aggregate(aggvars ~ hs10code_market, FUN = mean, data = mm_df)
colnames(agg) <- c("hs10code_market","year_mean","market_mean","impothpos_mean",
  "year_market_mean","year_impothpos_mean","market_impothpos_mean","year_market_impothpos_mean",
  "imp_mean")
mm_df <- join(mm_df, agg)
mm_df[,2:9] <- mm_df[,2:9] - mm_df[,12:19] 

mod1top25 <- lm(imp ~ year + year_market + year_impothpos + year_market_impothpos, data = mm_df)
mm_df$year_sector <- paste(newdat2$market, newdat2$year, substr(newdat2$hs10code,1,2),sep = "_")
mod2top25 <- lm(imp ~ year_impothpos + year_market_impothpos + year_sector, data= mm_df); 

## ALL
newdat <- data.frame(year = rep(c("20122016","20182020"), each = 2*nrow(trdmat_excl)), 
        hs10code = trdmat_excl$hs10code,
        imp = c(trdmat_excl$impchn20122016/5, trdmat_excl$impwor20122016/5, trdmat_excl$impchn20182020/3, trdmat_excl$impwor20182020/3)/1000,
        market = c(rep("China", nrow(trdmat_excl)), rep("Other", nrow(trdmat_excl)), rep("China", nrow(trdmat_excl)), rep("Other", nrow(trdmat_excl))),
        impothpos = c(trdmat_excl$impALLpos20122016, trdmat_excl$impALLpos20122016, trdmat_excl$impALLpos20122016, trdmat_excl$impALLpos20122016)
)
newdat$market <- factor(newdat$market, levels = c("Other","China"))

postrade <- trdmat_excl$hs10code[trdmat_excl$impchn20122016/5 > 0 | trdmat_excl$impwor20122016/5 > 0]
newdat2 <- newdat[newdat$hs10code %in% postrade,]
newdat2_ALL <- newdat2
mod <- lm(log(imp+1) ~ year*market*log(impothpos+1), data = newdat2);
mm_df <- data.frame(model.matrix(mod), imp = log(newdat2$imp+1), hs10code = newdat2$hs10code)
mm_df <- na.omit(mm_df)
colnames(mm_df) <- c("Intercept","year","market","impothpos",
  "year_market","year_impothpos","market_impothpos","year_market_impothpos",
  "imp","hs10code")
aggvars <- apply(mm_df[,colnames(mm_df)[2:9]], 2, as.numeric)
mm_df$hs10code_market <- paste(mm_df$hs10code,mm_df$market, sep = "")
agg <- aggregate(aggvars ~ hs10code_market, FUN = mean, data = mm_df)
colnames(agg) <- c("hs10code_market","year_mean","market_mean","impothpos_mean",
  "year_market_mean","year_impothpos_mean","market_impothpos_mean","year_market_impothpos_mean",
  "imp_mean")
mm_df <- join(mm_df, agg)
mm_df[,2:9] <- mm_df[,2:9] - mm_df[,12:19] 

mod1ALL <- lm(imp ~ year + year_market + year_impothpos + year_market_impothpos, data = mm_df)
mm_df$year_sector <- paste(newdat2$market, newdat2$year, substr(newdat2$hs10code,1,2),sep = "_")
mod2ALL <- lm(imp ~ year_impothpos + year_market_impothpos + year_sector, data= mm_df); 

## write models
hs6codes <- substr(newdat2_top10$hs10code, 1, 6)
mods_top10 <- list(coeftest(mod1top10, vcov = vcovCL, cluster = hs6codes)[1:5,1:4], 
  coeftest(mod2top10, vcov = vcovCL, cluster = hs6codes)[1:7,1:4])

vars <- c(rownames(mods_top10[[1]])[c(5)])
tab <- matrix(data = NA, ncol = length(mods_top10), nrow = length(vars)*2)
rownames(tab) <- c(rep(vars, each = 2))
for(i in 1:length(mods_top10)){mod <- mods_top10[[i]]; tabinmod <- unique(rownames(tab)[rownames(tab) %in% rownames(mod)])
stars <- rep("", length(tabinmod)); stars[mod[tabinmod,4] < .1] <- "^{+}"; stars[mod[tabinmod,4] < .05] <- "^{*}"; 
stars[mod[tabinmod,4] < .01] <- "^{**}"; stars[mod[tabinmod,4] < .001] <- "^{***}";  
tab[rownames(tab) %in% rownames(mod),i] <- c(rbind(paste(myround(mod[tabinmod,1],3), stars, sep = ""), paste("(", myround(mod[tabinmod,2],3), ")", sep = "")))}
rownames(tab) <- c(rbind(vars, "")); rownames(tab)[c(1)] <- 
  c("Post-Trade War$\\cdot$China$\\cdot$Alternative markets") 
N <- c(sum(summary(mod1top10)$df[1:2]),sum(summary(mod2top10)$df[1:2]))
N <- paste("\\multicolumn{1}{c}{", N, "}", sep = ""); tab <- rbind(tab, N)
addtorow <- list(); addtorow$pos <- list(2,3); addtorow$command <- c(paste0('\\midrule '),paste0(''))

tab1_top10_20182020 <- tab

hs6codes <- substr(newdat2_top25$hs10code, 1, 6)
mods_top25 <- list(coeftest(mod1top25, vcov = vcovCL, cluster = hs6codes)[1:5,1:4], 
  coeftest(mod2top25, vcov = vcovCL, cluster = hs6codes)[1:7,1:4])

vars <- c(rownames(mods_top25[[1]])[c(5)])
tab <- matrix(data = NA, ncol = length(mods_top25), nrow = length(vars)*2)
rownames(tab) <- c(rep(vars, each = 2))
for(i in 1:length(mods_top25)){mod <- mods_top25[[i]]; tabinmod <- unique(rownames(tab)[rownames(tab) %in% rownames(mod)])
stars <- rep("", length(tabinmod)); stars[mod[tabinmod,4] < .1] <- "^{+}"; stars[mod[tabinmod,4] < .05] <- "^{*}"; 
stars[mod[tabinmod,4] < .01] <- "^{**}"; stars[mod[tabinmod,4] < .001] <- "^{***}";  
tab[rownames(tab) %in% rownames(mod),i] <- c(rbind(paste(myround(mod[tabinmod,1],3), stars, sep = ""), paste("(", myround(mod[tabinmod,2],3), ")", sep = "")))}
rownames(tab) <- c(rbind(vars, "")); rownames(tab)[c(1)] <- 
  c("Post-Trade War$\\cdot$China$\\cdot$Alternative markets") 
N <- c(sum(summary(mod1top25)$df[1:2]),sum(summary(mod2top25)$df[1:2]))
N <- paste("\\multicolumn{1}{c}{", N, "}", sep = ""); tab <- rbind(tab, N)
addtorow <- list(); addtorow$pos <- list(2,3); addtorow$command <- c(paste0('\\midrule '),paste0(''))

tab1_top25_20182020 <- tab

hs6codes <- substr(newdat2_ALL$hs10code, 1, 6)
mods_ALL <- list(coeftest(mod1ALL, vcov = vcovCL, cluster = hs6codes)[1:5,1:4], 
  coeftest(mod2ALL, vcov = vcovCL, cluster = hs6codes)[1:7,1:4])

vars <- c(rownames(mods_ALL[[1]])[c(5)])
tab <- matrix(data = NA, ncol = length(mods_ALL), nrow = length(vars)*2)
rownames(tab) <- c(rep(vars, each = 2))
for(i in 1:length(mods_ALL)){mod <- mods_ALL[[i]]; tabinmod <- unique(rownames(tab)[rownames(tab) %in% rownames(mod)])
stars <- rep("", length(tabinmod)); stars[mod[tabinmod,4] < .1] <- "^{+}"; stars[mod[tabinmod,4] < .05] <- "^{*}"; 
stars[mod[tabinmod,4] < .01] <- "^{**}"; stars[mod[tabinmod,4] < .001] <- "^{***}";  
tab[rownames(tab) %in% rownames(mod),i] <- c(rbind(paste(myround(mod[tabinmod,1],3), stars, sep = ""), paste("(", myround(mod[tabinmod,2],3), ")", sep = "")))}
rownames(tab) <- c(rbind(vars, "")); rownames(tab)[c(1)] <- 
  c("Post-Trade War$\\cdot$China$\\cdot$Alternative markets") 
N <- c(sum(summary(mod1ALL)$df[1:2]),sum(summary(mod2ALL)$df[1:2]))
N <- paste("\\multicolumn{1}{c}{", N, "}", sep = ""); tab <- rbind(tab, N)
addtorow <- list(); addtorow$pos <- list(2,3); addtorow$command <- c(paste0('\\midrule '),paste0(''))

tab1_ALL_20182020 <- tab

######################################
## Models imports 20122016-20212023 ##
######################################

## top 10
newdat <- data.frame(year = rep(c("20122016","20212023"), each = 2*nrow(trdmat_excl)), 
        hs10code = trdmat_excl$hs10code, impoth = trdmat_excl$imptop1020122016,
        imp = c(trdmat_excl$impchn20122016/5, trdmat_excl$impwor20122016/5, trdmat_excl$impchn20212023/3, trdmat_excl$impwor20212023/3)/1000,
        market = c(rep("China", nrow(trdmat_excl)), rep("Other", nrow(trdmat_excl)), rep("China", nrow(trdmat_excl)), rep("Other", nrow(trdmat_excl))),
        impothpos = c(trdmat_excl$imptop10pos20122016, trdmat_excl$imptop10pos20122016, trdmat_excl$imptop10pos20122016, trdmat_excl$imptop10pos20122016)
)
newdat$market <- factor(newdat$market, levels = c("Other","China"))

postrade <- trdmat_excl$hs10code[trdmat_excl$impchn20122016/5 > 0 | trdmat_excl$impwor20122016/5 > 0]
newdat2 <- newdat[newdat$hs10code %in% postrade,]
newdat2_top10 <- newdat2
mod <- lm(log(imp+1) ~ year*market*I(impothpos/8), data = newdat2);
mm_df <- data.frame(model.matrix(mod), imp = log(newdat2$imp+1), hs10code = newdat2$hs10code)
mm_df <- na.omit(mm_df)
colnames(mm_df) <- c("Intercept","year","market","impothpos",
  "year_market","year_impothpos","market_impothpos","year_market_impothpos",
  "imp","hs10code")
aggvars <- apply(mm_df[,colnames(mm_df)[2:9]], 2, as.numeric)
mm_df$hs10code_market <- paste(mm_df$hs10code,mm_df$market, sep = "")
agg <- aggregate(aggvars ~ hs10code_market, FUN = mean, data = mm_df)
colnames(agg) <- c("hs10code_market","year_mean","market_mean","impothpos_mean",
  "year_market_mean","year_impothpos_mean","market_impothpos_mean","year_market_impothpos_mean",
  "imp_mean")
mm_df <- join(mm_df, agg)
mm_df[,2:9] <- mm_df[,2:9] - mm_df[,12:19] 

mod1top10 <- lm(imp ~ year + year_market + year_impothpos + year_market_impothpos, data = mm_df)
mm_df$year_sector <- paste(newdat2$market, newdat2$year, substr(newdat2$hs10code,1,2),sep = "_")
mod2top10 <- lm(imp ~ year_impothpos + year_market_impothpos + year_sector, data= mm_df); 

## top 25
newdat <- data.frame(year = rep(c("20122016","20212023"), each = 2*nrow(trdmat_excl)), 
        hs10code = trdmat_excl$hs10code,
        imp = c(trdmat_excl$impchn20122016/5, trdmat_excl$impwor20122016/5, trdmat_excl$impchn20212023/3, trdmat_excl$impwor20212023/3)/1000,
        market = c(rep("China", nrow(trdmat_excl)), rep("Other", nrow(trdmat_excl)), rep("China", nrow(trdmat_excl)), rep("Other", nrow(trdmat_excl))),
        impothpos = c(trdmat_excl$imptop25pos20122016, trdmat_excl$imptop25pos20122016, trdmat_excl$imptop25pos20122016, trdmat_excl$imptop25pos20122016)
)
newdat$market <- factor(newdat$market, levels = c("Other","China"))

postrade <- trdmat_excl$hs10code[trdmat_excl$impchn20122016/5 > 0 | trdmat_excl$impwor20122016/5 > 0]
newdat2 <- newdat[newdat$hs10code %in% postrade,]
newdat2_top25 <- newdat2
mod <- lm(log(imp+1) ~ year*market*log(impothpos+1), data = newdat2);
mm_df <- data.frame(model.matrix(mod), imp = log(newdat2$imp+1), hs10code = newdat2$hs10code)
mm_df <- na.omit(mm_df)
colnames(mm_df) <- c("Intercept","year","market","impothpos",
  "year_market","year_impothpos","market_impothpos","year_market_impothpos",
  "imp","hs10code")
aggvars <- apply(mm_df[,colnames(mm_df)[2:9]], 2, as.numeric)
mm_df$hs10code_market <- paste(mm_df$hs10code,mm_df$market, sep = "")
agg <- aggregate(aggvars ~ hs10code_market, FUN = mean, data = mm_df)
colnames(agg) <- c("hs10code_market","year_mean","market_mean","impothpos_mean",
  "year_market_mean","year_impothpos_mean","market_impothpos_mean","year_market_impothpos_mean",
  "imp_mean")
mm_df <- join(mm_df, agg)
mm_df[,2:9] <- mm_df[,2:9] - mm_df[,12:19] 

mod1top25 <- lm(imp ~ year + year_market + year_impothpos + year_market_impothpos, data = mm_df)
mm_df$year_sector <- paste(newdat2$market, newdat2$year, substr(newdat2$hs10code,1,2),sep = "_")
mod2top25 <- lm(imp ~ year_impothpos + year_market_impothpos + year_sector, data= mm_df); 

## ALL
newdat <- data.frame(year = rep(c("20122016","20212023"), each = 2*nrow(trdmat_excl)), 
        hs10code = trdmat_excl$hs10code,
        imp = c(trdmat_excl$impchn20122016/5, trdmat_excl$impwor20122016/5, trdmat_excl$impchn20212023/3, trdmat_excl$impwor20212023/3)/1000,
        market = c(rep("China", nrow(trdmat_excl)), rep("Other", nrow(trdmat_excl)), rep("China", nrow(trdmat_excl)), rep("Other", nrow(trdmat_excl))),
        impothpos = c(trdmat_excl$impALLpos20122016, trdmat_excl$impALLpos20122016, trdmat_excl$impALLpos20122016, trdmat_excl$impALLpos20122016)
)
newdat$market <- factor(newdat$market, levels = c("Other","China"))

postrade <- trdmat_excl$hs10code[trdmat_excl$impchn20122016/5 > 0 | trdmat_excl$impwor20122016/5 > 0]
newdat2 <- newdat[newdat$hs10code %in% postrade,]
newdat2_ALL <- newdat2
mod <- lm(log(imp+1) ~ year*market*log(impothpos+1), data = newdat2);
mm_df <- data.frame(model.matrix(mod), imp = log(newdat2$imp+1), hs10code = newdat2$hs10code)
mm_df <- na.omit(mm_df)
colnames(mm_df) <- c("Intercept","year","market","impothpos",
  "year_market","year_impothpos","market_impothpos","year_market_impothpos",
  "imp","hs10code")
aggvars <- apply(mm_df[,colnames(mm_df)[2:9]], 2, as.numeric)
mm_df$hs10code_market <- paste(mm_df$hs10code,mm_df$market, sep = "")
agg <- aggregate(aggvars ~ hs10code_market, FUN = mean, data = mm_df)
colnames(agg) <- c("hs10code_market","year_mean","market_mean","impothpos_mean",
  "year_market_mean","year_impothpos_mean","market_impothpos_mean","year_market_impothpos_mean",
  "imp_mean")
mm_df <- join(mm_df, agg)
mm_df[,2:9] <- mm_df[,2:9] - mm_df[,12:19] 

mod1ALL <- lm(imp ~ year + year_market + year_impothpos + year_market_impothpos, data = mm_df)
mm_df$year_sector <- paste(newdat2$market, newdat2$year, substr(newdat2$hs10code,1,2),sep = "_")
mod2ALL <- lm(imp ~ year_impothpos + year_market_impothpos + year_sector, data= mm_df); 

## write models
hs6codes <- substr(newdat2_top10$hs10code, 1, 6)
mods_top10 <- list(coeftest(mod1top10, vcov = vcovCL, cluster = hs6codes)[1:5,1:4], 
  coeftest(mod2top10, vcov = vcovCL, cluster = hs6codes)[1:7,1:4])

vars <- c(rownames(mods_top10[[1]])[c(5)])
tab <- matrix(data = NA, ncol = length(mods_top10), nrow = length(vars)*2)
rownames(tab) <- c(rep(vars, each = 2))
for(i in 1:length(mods_top10)){mod <- mods_top10[[i]]; tabinmod <- unique(rownames(tab)[rownames(tab) %in% rownames(mod)])
stars <- rep("", length(tabinmod)); stars[mod[tabinmod,4] < .1] <- "^{+}"; stars[mod[tabinmod,4] < .05] <- "^{*}"; 
stars[mod[tabinmod,4] < .01] <- "^{**}"; stars[mod[tabinmod,4] < .001] <- "^{***}";  
tab[rownames(tab) %in% rownames(mod),i] <- c(rbind(paste(myround(mod[tabinmod,1],3), stars, sep = ""), paste("(", myround(mod[tabinmod,2],3), ")", sep = "")))}
rownames(tab) <- c(rbind(vars, "")); rownames(tab)[c(1)] <- 
  c("Post-Trade War$\\cdot$China$\\cdot$Alternative markets") 
N <- c(sum(summary(mod1top10)$df[1:2]),sum(summary(mod2top10)$df[1:2]))
N <- paste("\\multicolumn{1}{c}{", N, "}", sep = ""); tab <- rbind(tab, N)
addtorow <- list(); addtorow$pos <- list(2,3); addtorow$command <- c(paste0('\\midrule '),paste0(''))
tab1_top10_20212023 <- tab

hs6codes <- substr(newdat2_top25$hs10code, 1, 6)
mods_top25 <- list(coeftest(mod1top25, vcov = vcovCL, cluster = hs6codes)[1:5,1:4], 
  coeftest(mod2top25, vcov = vcovCL, cluster = hs6codes)[1:7,1:4])

vars <- c(rownames(mods_top25[[1]])[c(5)])
tab <- matrix(data = NA, ncol = length(mods_top25), nrow = length(vars)*2)
rownames(tab) <- c(rep(vars, each = 2))
for(i in 1:length(mods_top25)){mod <- mods_top25[[i]]; tabinmod <- unique(rownames(tab)[rownames(tab) %in% rownames(mod)])
stars <- rep("", length(tabinmod)); stars[mod[tabinmod,4] < .1] <- "^{+}"; stars[mod[tabinmod,4] < .05] <- "^{*}"; 
stars[mod[tabinmod,4] < .01] <- "^{**}"; stars[mod[tabinmod,4] < .001] <- "^{***}";  
tab[rownames(tab) %in% rownames(mod),i] <- c(rbind(paste(myround(mod[tabinmod,1],3), stars, sep = ""), paste("(", myround(mod[tabinmod,2],3), ")", sep = "")))}
rownames(tab) <- c(rbind(vars, "")); rownames(tab)[c(1)] <- 
  c("Post-Trade War$\\cdot$China$\\cdot$Alternative markets") 
N <- c(sum(summary(mod1top25)$df[1:2]),sum(summary(mod2top25)$df[1:2]))
N <- paste("\\multicolumn{1}{c}{", N, "}", sep = ""); tab <- rbind(tab, N)
addtorow <- list(); addtorow$pos <- list(2,3); addtorow$command <- c(paste0('\\midrule '),paste0(''))
tab1_top25_20212023 <- tab

hs6codes <- substr(newdat2_ALL$hs10code, 1, 6)
mods_ALL <- list(coeftest(mod1ALL, vcov = vcovCL, cluster = hs6codes)[1:5,1:4], 
  coeftest(mod2ALL, vcov = vcovCL, cluster = hs6codes)[1:7,1:4])

vars <- c(rownames(mods_ALL[[1]])[c(5)])
tab <- matrix(data = NA, ncol = length(mods_ALL), nrow = length(vars)*2)
rownames(tab) <- c(rep(vars, each = 2))
for(i in 1:length(mods_ALL)){mod <- mods_ALL[[i]]; tabinmod <- unique(rownames(tab)[rownames(tab) %in% rownames(mod)])
stars <- rep("", length(tabinmod)); stars[mod[tabinmod,4] < .1] <- "^{+}"; stars[mod[tabinmod,4] < .05] <- "^{*}"; 
stars[mod[tabinmod,4] < .01] <- "^{**}"; stars[mod[tabinmod,4] < .001] <- "^{***}";  
tab[rownames(tab) %in% rownames(mod),i] <- c(rbind(paste(myround(mod[tabinmod,1],3), stars, sep = ""), paste("(", myround(mod[tabinmod,2],3), ")", sep = "")))}
rownames(tab) <- c(rbind(vars, "")); rownames(tab)[c(1)] <- 
  c("Post-Trade War$\\cdot$China$\\cdot$Alternative markets") 
N <- c(sum(summary(mod1ALL)$df[1:2]),sum(summary(mod2ALL)$df[1:2]))
N <- paste("\\multicolumn{1}{c}{", N, "}", sep = ""); tab <- rbind(tab, N)
addtorow <- list(); addtorow$pos <- list(2,3); addtorow$command <- c(paste0('\\midrule '),paste0(''))
tab1_ALL_20212023 <- tab

ptable(cbind(rownames(tab), tab1_top10_20182020, tab1_top10_20212023), "paper/tables/TableA5top.tex")
ptable(cbind(rownames(tab), tab1_top25_20182020, tab1_top25_20212023), "paper/tables/TableA5mid.tex")
ptable(cbind(rownames(tab), tab1_ALL_20182020, tab1_ALL_20212023), "paper/tables/TableA5bot.tex")

########################################
## Trade models allies and non-allies ##
########################################

# summary stats on trade with allies and non-allies
sum(trdmat_excl$impALL20122016allies + trdmat_excl$impALL20182020allies)/sum(trdmat_excl$impALL20122016 + trdmat_excl$impALL20182020)
sum(trdmat_excl$impALL20122016nonallies + trdmat_excl$impALL20182020nonallies)/sum(trdmat_excl$impALL20122016 + trdmat_excl$impALL20182020)

## ALL
newdat <- data.frame(year = rep(c("20122016","20182020"), each = 2*nrow(trdmat_excl)), 
        hs10code = trdmat_excl$hs10code,
        imp = c(trdmat_excl$impchn20122016/5, trdmat_excl$impALL20122016/5, trdmat_excl$impchn20182020/3, trdmat_excl$impALL20182020/3)/1000,
        impallies = c(trdmat_excl$impchn20122016/5, trdmat_excl$impALL20122016allies/5, trdmat_excl$impchn20182020/3, trdmat_excl$impALL20182020allies/3)/1000,
        impnonallies = c(trdmat_excl$impchn20122016/5, trdmat_excl$impALL20122016nonallies/5, trdmat_excl$impchn20182020/3, trdmat_excl$impALL20182020nonallies/3)/1000,
        market = c(rep("China", nrow(trdmat_excl)), rep("Other", nrow(trdmat_excl)), rep("China", nrow(trdmat_excl)), rep("Other", nrow(trdmat_excl))),
        impothpos = c(trdmat_excl$impALLpos20122016, trdmat_excl$impALLpos20122016, trdmat_excl$impALLpos20122016, trdmat_excl$impALLpos20122016),
        impothposallies = c(trdmat_excl$impALLpos20122016allies, trdmat_excl$impALLpos20122016allies, trdmat_excl$impALLpos20122016allies, trdmat_excl$impALLpos20122016allies),
        impothposnonallies = c(trdmat_excl$impALLpos20122016nonallies, trdmat_excl$impALLpos20122016nonallies, trdmat_excl$impALLpos20122016nonallies, trdmat_excl$impALLpos20122016nonallies)
)
newdat$market <- factor(newdat$market, levels = c("Other","China"))

postrade <- trdmat_excl$hs10code[trdmat_excl$impchn20122016/5 > 0 | trdmat_excl$impwor20122016/5 > 0]
newdat2 <- newdat[newdat$hs10code %in% postrade,]
newdat2_ALL <- newdat2
mod <- lm(log(impallies+1) ~ year*market*log(impothposallies+1), data = newdat2);
mm_df <- data.frame(model.matrix(mod), impallies = log(newdat2$impallies+1), hs10code = newdat2$hs10code)
mm_df <- na.omit(mm_df)
colnames(mm_df) <- c("Intercept","year","market","impothposallies",
  "year_market","year_impothposallies","market_impothposallies","year_market_impothposallies",
  "impallies","hs10code")
aggvars <- apply(mm_df[,colnames(mm_df)[2:9]], 2, as.numeric)
mm_df$hs10code_market <- paste(mm_df$hs10code,mm_df$market, sep = "")
agg <- aggregate(aggvars ~ hs10code_market, FUN = mean, data = mm_df)
colnames(agg) <- c("hs10code_market","year_mean","market_mean","impothposallies_mean",
  "year_market_mean","year_impothposallies_mean","market_impothposallies_mean","year_market_impothposallies_mean",
  "impallies_mean")
mm_df <- join(mm_df, agg)
mm_df[,2:9] <- mm_df[,2:9] - mm_df[,12:19] 

mod1ALLallies <- lm(impallies ~ year + year_market + year_impothposallies + year_market_impothposallies, data = mm_df)
mm_df$year_sector <- paste(newdat2$market, newdat2$year, substr(newdat2$hs10code,1,2),sep = "_")
mod2ALLallies <- lm(impallies ~ year_impothposallies + year_market_impothposallies + year_sector, data= mm_df); 

postrade <- trdmat_excl$hs10code[trdmat_excl$impchn20122016/5 > 0 | trdmat_excl$impwor20122016/5 > 0]
newdat2 <- newdat[newdat$hs10code %in% postrade,]
newdat2_ALL <- newdat2
mod <- lm(log(impnonallies+1) ~ year*market*log(impothposnonallies+1), data = newdat2);
mm_df <- data.frame(model.matrix(mod), impnonallies = log(newdat2$impnonallies+1), hs10code = newdat2$hs10code)
mm_df <- na.omit(mm_df)
colnames(mm_df) <- c("Intercept","year","market","impothposnonallies",
  "year_market","year_impothposnonallies","market_impothposnonallies","year_market_impothposnonallies",
  "impnonallies","hs10code")
aggvars <- apply(mm_df[,colnames(mm_df)[2:9]], 2, as.numeric)
mm_df$hs10code_market <- paste(mm_df$hs10code,mm_df$market, sep = "")
agg <- aggregate(aggvars ~ hs10code_market, FUN = mean, data = mm_df)
colnames(agg) <- c("hs10code_market","year_mean","market_mean","impothposnonallies_mean",
  "year_market_mean","year_impothposnonallies_mean","market_impothposnonallies_mean","year_market_impothposnonallies_mean",
  "impnonallies_mean")
mm_df <- join(mm_df, agg)
mm_df[,2:9] <- mm_df[,2:9] - mm_df[,12:19] 

mod1ALLnonallies <- lm(impnonallies ~ year + year_market + year_impothposnonallies + year_market_impothposnonallies, data = mm_df)
mm_df$year_sector <- paste(newdat2$market, newdat2$year, substr(newdat2$hs10code,1,2),sep = "_")
mod2ALLnonallies <- lm(impnonallies ~ year_impothposnonallies + year_market_impothposnonallies + year_sector, data= mm_df); 

## write models
hs6codes <- substr(newdat2_ALL$hs10code, 1, 6)
mods_ALLallies <- list(coeftest(mod1ALLallies, vcov = vcovCL, cluster = hs6codes)[1:5,1:4], 
  coeftest(mod2ALLallies, vcov = vcovCL, cluster = hs6codes)[1:7,1:4])

vars <- c(rownames(mods_ALLallies[[1]])[c(5)])
tab <- matrix(data = NA, ncol = length(mods_ALLallies), nrow = length(vars)*2)
rownames(tab) <- c(rep(vars, each = 2))
for(i in 1:length(mods_ALLallies)){mod <- mods_ALLallies[[i]]; tabinmod <- unique(rownames(tab)[rownames(tab) %in% rownames(mod)])
stars <- rep("", length(tabinmod)); stars[mod[tabinmod,4] < .1] <- "^{+}"; stars[mod[tabinmod,4] < .05] <- "^{*}"; 
stars[mod[tabinmod,4] < .01] <- "^{**}"; stars[mod[tabinmod,4] < .001] <- "^{***}";  
tab[rownames(tab) %in% rownames(mod),i] <- c(rbind(paste(myround(mod[tabinmod,1],3), stars, sep = ""), paste("(", myround(mod[tabinmod,2],3), ")", sep = "")))}
rownames(tab) <- c(rbind(vars, "")); rownames(tab)[c(1)] <- 
  c("Post-Trade War$\\cdot$China$\\cdot$Allied alternative markets") 
N <- c(sum(summary(mod1ALLallies)$df[1:2]),sum(summary(mod2ALLallies)$df[1:2]))
N <- paste("\\multicolumn{1}{c}{", N, "}", sep = ""); tab <- rbind(tab, N)
addtorow <- list(); addtorow$pos <- list(2,3); addtorow$command <- c(paste0('\\midrule '),paste0(''))
tab1 <- tab

hs6codes <- substr(newdat2_ALL$hs10code, 1, 6)
mods_ALLnonallies <- list(coeftest(mod1ALLnonallies, vcov = vcovCL, cluster = hs6codes)[1:5,1:4], 
  coeftest(mod2ALLnonallies, vcov = vcovCL, cluster = hs6codes)[1:7,1:4])

vars <- c(rownames(mods_ALLnonallies[[1]])[c(5)])
tab <- matrix(data = NA, ncol = length(mods_ALLnonallies), nrow = length(vars)*2)
rownames(tab) <- c(rep(vars, each = 2))
for(i in 1:length(mods_ALLnonallies)){mod <- mods_ALLnonallies[[i]]; tabinmod <- unique(rownames(tab)[rownames(tab) %in% rownames(mod)])
stars <- rep("", length(tabinmod)); stars[mod[tabinmod,4] < .1] <- "^{+}"; stars[mod[tabinmod,4] < .05] <- "^{*}"; 
stars[mod[tabinmod,4] < .01] <- "^{**}"; stars[mod[tabinmod,4] < .001] <- "^{***}";  
tab[rownames(tab) %in% rownames(mod),i] <- c(rbind(paste(myround(mod[tabinmod,1],3), stars, sep = ""), paste("(", myround(mod[tabinmod,2],3), ")", sep = "")))}
rownames(tab) <- c(rbind(vars, "")); rownames(tab)[c(1)] <- 
  c("Post-Trade War$\\cdot$China$\\cdot$Non-allied alternative markets") 
N <- c(sum(summary(mod1ALLnonallies)$df[1:2]),sum(summary(mod2ALLnonallies)$df[1:2]))
N <- paste("\\multicolumn{1}{c}{", N, "}", sep = ""); tab <- rbind(tab, N)
addtorow <- list(); addtorow$pos <- list(2,3); addtorow$command <- c(paste0('\\midrule '),paste0(''))
tab2 <- tab

mrg_rw <- rownames(tab); mrg_rw[1] <-  "Post-Trade War$\\cdot$China$\\cdot$Alternative markets"
tab2_ALL_20182020_allies <- tab1
tab2_ALL_20182020_nonallies <- tab2

##########################################
## Trade models friends and non-friends ##
##########################################

# summary stats on trade with friends and non-friends
sum(trdmat_excl$impALL20122016friends + trdmat_excl$impALL20182020friends)/sum(trdmat_excl$impALL20122016 + trdmat_excl$impALL20182020)
sum(trdmat_excl$impALL20122016nonfriends + trdmat_excl$impALL20182020nonfriends)/sum(trdmat_excl$impALL20122016 + trdmat_excl$impALL20182020)

## ALL
newdat <- data.frame(year = rep(c("20122016","20182020"), each = 2*nrow(trdmat_excl)), 
        hs10code = trdmat_excl$hs10code,
        imp = c(trdmat_excl$impchn20122016/5, trdmat_excl$impALL20122016/5, trdmat_excl$impchn20182020/3, trdmat_excl$impALL20182020/3)/1000,
        impfriends = c(trdmat_excl$impchn20122016/5, trdmat_excl$impALL20122016friends/5, trdmat_excl$impchn20182020/3, trdmat_excl$impALL20182020friends/3)/1000,
        impnonfriends = c(trdmat_excl$impchn20122016/5, trdmat_excl$impALL20122016nonfriends/5, trdmat_excl$impchn20182020/3, trdmat_excl$impALL20182020nonfriends/3)/1000,
        market = c(rep("China", nrow(trdmat_excl)), rep("Other", nrow(trdmat_excl)), rep("China", nrow(trdmat_excl)), rep("Other", nrow(trdmat_excl))),
        impothpos = c(trdmat_excl$impALLpos20122016, trdmat_excl$impALLpos20122016, trdmat_excl$impALLpos20122016, trdmat_excl$impALLpos20122016),
        impothposfriends = c(trdmat_excl$impALLpos20122016friends, trdmat_excl$impALLpos20122016friends, trdmat_excl$impALLpos20122016friends, trdmat_excl$impALLpos20122016friends),
        impothposnonfriends = c(trdmat_excl$impALLpos20122016nonfriends, trdmat_excl$impALLpos20122016nonfriends, trdmat_excl$impALLpos20122016nonfriends, trdmat_excl$impALLpos20122016nonfriends)
)
newdat$market <- factor(newdat$market, levels = c("Other","China"))

postrade <- trdmat_excl$hs10code[trdmat_excl$impchn20122016/5 > 0 | trdmat_excl$impwor20122016/5 > 0]
newdat2 <- newdat[newdat$hs10code %in% postrade,]
newdat2_ALL <- newdat2
mod <- lm(log(impfriends+1) ~ year*market*log(impothposfriends+1), data = newdat2);
mm_df <- data.frame(model.matrix(mod), impfriends = log(newdat2$impfriends+1), hs10code = newdat2$hs10code)
mm_df <- na.omit(mm_df)
colnames(mm_df) <- c("Intercept","year","market","impothposfriends",
  "year_market","year_impothposfriends","market_impothposfriends","year_market_impothposfriends",
  "impfriends","hs10code")
aggvars <- apply(mm_df[,colnames(mm_df)[2:9]], 2, as.numeric)
mm_df$hs10code_market <- paste(mm_df$hs10code,mm_df$market, sep = "")
agg <- aggregate(aggvars ~ hs10code_market, FUN = mean, data = mm_df)
colnames(agg) <- c("hs10code_market","year_mean","market_mean","impothposfriends_mean",
  "year_market_mean","year_impothposfriends_mean","market_impothposfriends_mean","year_market_impothposfriends_mean",
  "impfriends_mean")
mm_df <- join(mm_df, agg)
mm_df[,2:9] <- mm_df[,2:9] - mm_df[,12:19] 

mod1ALLfriends <- lm(impfriends ~ year + year_market + year_impothposfriends + year_market_impothposfriends, data = mm_df)
mm_df$year_sector <- paste(newdat2$market, newdat2$year, substr(newdat2$hs10code,1,2),sep = "_")
mod2ALLfriends <- lm(impfriends ~ year_impothposfriends + year_market_impothposfriends + year_sector, data= mm_df); 

postrade <- trdmat_excl$hs10code[trdmat_excl$impchn20122016/5 > 0 | trdmat_excl$impwor20122016/5 > 0]
newdat2 <- newdat[newdat$hs10code %in% postrade,]
newdat2_ALL <- newdat2
mod <- lm(log(impnonfriends+1) ~ year*market*log(impothposnonfriends+1), data = newdat2);
mm_df <- data.frame(model.matrix(mod), impnonfriends = log(newdat2$impnonfriends+1), hs10code = newdat2$hs10code)
mm_df <- na.omit(mm_df)
colnames(mm_df) <- c("Intercept","year","market","impothposnonfriends",
  "year_market","year_impothposnonfriends","market_impothposnonfriends","year_market_impothposnonfriends",
  "impnonfriends","hs10code")
aggvars <- apply(mm_df[,colnames(mm_df)[2:9]], 2, as.numeric)
mm_df$hs10code_market <- paste(mm_df$hs10code,mm_df$market, sep = "")
agg <- aggregate(aggvars ~ hs10code_market, FUN = mean, data = mm_df)
colnames(agg) <- c("hs10code_market","year_mean","market_mean","impothposnonfriends_mean",
  "year_market_mean","year_impothposnonfriends_mean","market_impothposnonfriends_mean","year_market_impothposnonfriends_mean",
  "impnonfriends_mean")
mm_df <- join(mm_df, agg)
mm_df[,2:9] <- mm_df[,2:9] - mm_df[,12:19] 

mod1ALLnonfriends <- lm(impnonfriends ~ year + year_market + year_impothposnonfriends + year_market_impothposnonfriends, data = mm_df)
mm_df$year_sector <- paste(newdat2$market, newdat2$year, substr(newdat2$hs10code,1,2),sep = "_")
mod2ALLnonfriends <- lm(impnonfriends ~ year_impothposnonfriends + year_market_impothposnonfriends + year_sector, data= mm_df); 

## write models
hs6codes <- substr(newdat2_ALL$hs10code, 1, 6)
mods_ALLfriends <- list(coeftest(mod1ALLfriends, vcov = vcovCL, cluster = hs6codes)[1:5,1:4], 
  coeftest(mod2ALLfriends, vcov = vcovCL, cluster = hs6codes)[1:7,1:4])

vars <- c(rownames(mods_ALLfriends[[1]])[c(5)])
tab <- matrix(data = NA, ncol = length(mods_ALLfriends), nrow = length(vars)*2)
rownames(tab) <- c(rep(vars, each = 2))
for(i in 1:length(mods_ALLfriends)){mod <- mods_ALLfriends[[i]]; tabinmod <- unique(rownames(tab)[rownames(tab) %in% rownames(mod)])
stars <- rep("", length(tabinmod)); stars[mod[tabinmod,4] < .1] <- "^{+}"; stars[mod[tabinmod,4] < .05] <- "^{*}"; 
stars[mod[tabinmod,4] < .01] <- "^{**}"; stars[mod[tabinmod,4] < .001] <- "^{***}";  
tab[rownames(tab) %in% rownames(mod),i] <- c(rbind(paste(myround(mod[tabinmod,1],3), stars, sep = ""), paste("(", myround(mod[tabinmod,2],3), ")", sep = "")))}
rownames(tab) <- c(rbind(vars, "")); rownames(tab)[c(1)] <- 
  c("Post-Trade War$\\cdot$China$\\cdot$Friendly alternative markets") 
N <- c(sum(summary(mod1ALLfriends)$df[1:2]),sum(summary(mod2ALLfriends)$df[1:2]))
N <- paste("\\multicolumn{1}{c}{", N, "}", sep = ""); tab <- rbind(tab, N)
addtorow <- list(); addtorow$pos <- list(2,3); addtorow$command <- c(paste0('\\midrule '),paste0(''))
tab1 <- tab

hs6codes <- substr(newdat2_ALL$hs10code, 1, 6)
mods_ALLnonfriends <- list(coeftest(mod1ALLnonfriends, vcov = vcovCL, cluster = hs6codes)[1:5,1:4], 
  coeftest(mod2ALLnonfriends, vcov = vcovCL, cluster = hs6codes)[1:7,1:4])

vars <- c(rownames(mods_ALLnonfriends[[1]])[c(5)])
tab <- matrix(data = NA, ncol = length(mods_ALLnonfriends), nrow = length(vars)*2)
rownames(tab) <- c(rep(vars, each = 2))
for(i in 1:length(mods_ALLnonfriends)){mod <- mods_ALLnonfriends[[i]]; tabinmod <- unique(rownames(tab)[rownames(tab) %in% rownames(mod)])
stars <- rep("", length(tabinmod)); stars[mod[tabinmod,4] < .1] <- "^{+}"; stars[mod[tabinmod,4] < .05] <- "^{*}"; 
stars[mod[tabinmod,4] < .01] <- "^{**}"; stars[mod[tabinmod,4] < .001] <- "^{***}";  
tab[rownames(tab) %in% rownames(mod),i] <- c(rbind(paste(myround(mod[tabinmod,1],3), stars, sep = ""), paste("(", myround(mod[tabinmod,2],3), ")", sep = "")))}
rownames(tab) <- c(rbind(vars, "")); rownames(tab)[c(1)] <- 
  c("Post-Trade War$\\cdot$China$\\cdot$Non-friendly alternative markets") 
N <- c(sum(summary(mod1ALLnonfriends)$df[1:2]),sum(summary(mod2ALLnonfriends)$df[1:2]))
N <- paste("\\multicolumn{1}{c}{", N, "}", sep = ""); tab <- rbind(tab, N)
addtorow <- list(); addtorow$pos <- list(2,3); addtorow$command <- c(paste0('\\midrule '),paste0(''))
tab2 <- tab

mrg_rw <- rownames(tab); mrg_rw[1] <-  "Post-Trade War$\\cdot$China$\\cdot$Alternative markets"
tab2_ALL_20182020_friends <- tab1
tab2_ALL_20182020_nonfriends <- tab2

#################################################
## Trade models allies and non-allies 20212023 ##
#################################################

## ALL
newdat <- data.frame(year = rep(c("20122016","20212023"), each = 2*nrow(trdmat_excl)), 
        hs10code = trdmat_excl$hs10code,
        imp = c(trdmat_excl$impchn20122016/5, trdmat_excl$impALL20122016/5, trdmat_excl$impchn20212023/3, trdmat_excl$impALL20212023/3)/1000,
        impallies = c(trdmat_excl$impchn20122016/5, trdmat_excl$impALL20122016allies/5, trdmat_excl$impchn20212023/3, trdmat_excl$impALL20212023allies/3)/1000,
        impnonallies = c(trdmat_excl$impchn20122016/5, trdmat_excl$impALL20122016nonallies/5, trdmat_excl$impchn20212023/3, trdmat_excl$impALL20212023nonallies/3)/1000,
        market = c(rep("China", nrow(trdmat_excl)), rep("Other", nrow(trdmat_excl)), rep("China", nrow(trdmat_excl)), rep("Other", nrow(trdmat_excl))),
        impothpos = c(trdmat_excl$impALLpos20122016, trdmat_excl$impALLpos20122016, trdmat_excl$impALLpos20122016, trdmat_excl$impALLpos20122016),
        impothposallies = c(trdmat_excl$impALLpos20122016allies, trdmat_excl$impALLpos20122016allies, trdmat_excl$impALLpos20122016allies, trdmat_excl$impALLpos20122016allies),
        impothposnonallies = c(trdmat_excl$impALLpos20122016nonallies, trdmat_excl$impALLpos20122016nonallies, trdmat_excl$impALLpos20122016nonallies, trdmat_excl$impALLpos20122016nonallies)
)
newdat$market <- factor(newdat$market, levels = c("Other","China"))

postrade <- trdmat_excl$hs10code[trdmat_excl$impchn20122016/5 > 0 | trdmat_excl$impwor20122016/5 > 0]
newdat2 <- newdat[newdat$hs10code %in% postrade,]
newdat2_ALL <- newdat2
mod <- lm(log(impallies+1) ~ year*market*log(impothposallies+1), data = newdat2);
mm_df <- data.frame(model.matrix(mod), impallies = log(newdat2$impallies+1), hs10code = newdat2$hs10code)
mm_df <- na.omit(mm_df)
colnames(mm_df) <- c("Intercept","year","market","impothposallies",
  "year_market","year_impothposallies","market_impothposallies","year_market_impothposallies",
  "impallies","hs10code")
aggvars <- apply(mm_df[,colnames(mm_df)[2:9]], 2, as.numeric)
mm_df$hs10code_market <- paste(mm_df$hs10code,mm_df$market, sep = "")
agg <- aggregate(aggvars ~ hs10code_market, FUN = mean, data = mm_df)
colnames(agg) <- c("hs10code_market","year_mean","market_mean","impothposallies_mean",
  "year_market_mean","year_impothposallies_mean","market_impothposallies_mean","year_market_impothposallies_mean",
  "impallies_mean")
mm_df <- join(mm_df, agg)
mm_df[,2:9] <- mm_df[,2:9] - mm_df[,12:19] 

mod1ALLallies <- lm(impallies ~ year + year_market + year_impothposallies + year_market_impothposallies, data = mm_df)
mm_df$year_sector <- paste(newdat2$market, newdat2$year, substr(newdat2$hs10code,1,2),sep = "_")
mod2ALLallies <- lm(impallies ~ year_impothposallies + year_market_impothposallies + year_sector, data= mm_df); 

postrade <- trdmat_excl$hs10code[trdmat_excl$impchn20122016/5 > 0 | trdmat_excl$impwor20122016/5 > 0]
newdat2 <- newdat[newdat$hs10code %in% postrade,]
newdat2_ALL <- newdat2
mod <- lm(log(impnonallies+1) ~ year*market*log(impothposnonallies+1), data = newdat2);
mm_df <- data.frame(model.matrix(mod), impnonallies = log(newdat2$impnonallies+1), hs10code = newdat2$hs10code)
mm_df <- na.omit(mm_df)
colnames(mm_df) <- c("Intercept","year","market","impothposnonallies",
  "year_market","year_impothposnonallies","market_impothposnonallies","year_market_impothposnonallies",
  "impnonallies","hs10code")
aggvars <- apply(mm_df[,colnames(mm_df)[2:9]], 2, as.numeric)
mm_df$hs10code_market <- paste(mm_df$hs10code,mm_df$market, sep = "")
agg <- aggregate(aggvars ~ hs10code_market, FUN = mean, data = mm_df)
colnames(agg) <- c("hs10code_market","year_mean","market_mean","impothposnonallies_mean",
  "year_market_mean","year_impothposnonallies_mean","market_impothposnonallies_mean","year_market_impothposnonallies_mean",
  "impnonallies_mean")
mm_df <- join(mm_df, agg)
mm_df[,2:9] <- mm_df[,2:9] - mm_df[,12:19] 

mod1ALLnonallies <- lm(impnonallies ~ year + year_market + year_impothposnonallies + year_market_impothposnonallies, data = mm_df)
mm_df$year_sector <- paste(newdat2$market, newdat2$year, substr(newdat2$hs10code,1,2),sep = "_")
mod2ALLnonallies <- lm(impnonallies ~ year_impothposnonallies + year_market_impothposnonallies + year_sector, data= mm_df); 

## write models
hs6codes <- substr(newdat2_ALL$hs10code, 1, 6)
mods_ALLallies <- list(coeftest(mod1ALLallies, vcov = vcovCL, cluster = hs6codes)[1:5,1:4], 
  coeftest(mod2ALLallies, vcov = vcovCL, cluster = hs6codes)[1:7,1:4])

vars <- c(rownames(mods_ALLallies[[1]])[c(5)])
tab <- matrix(data = NA, ncol = length(mods_ALLallies), nrow = length(vars)*2)
rownames(tab) <- c(rep(vars, each = 2))
for(i in 1:length(mods_ALLallies)){mod <- mods_ALLallies[[i]]; tabinmod <- unique(rownames(tab)[rownames(tab) %in% rownames(mod)])
stars <- rep("", length(tabinmod)); stars[mod[tabinmod,4] < .1] <- "^{+}"; stars[mod[tabinmod,4] < .05] <- "^{*}"; 
stars[mod[tabinmod,4] < .01] <- "^{**}"; stars[mod[tabinmod,4] < .001] <- "^{***}";  
tab[rownames(tab) %in% rownames(mod),i] <- c(rbind(paste(myround(mod[tabinmod,1],3), stars, sep = ""), paste("(", myround(mod[tabinmod,2],3), ")", sep = "")))}
rownames(tab) <- c(rbind(vars, "")); rownames(tab)[c(1)] <- 
  c("Post-Trade War$\\cdot$China$\\cdot$Allied alternative markets") 
N <- c(sum(summary(mod1ALLallies)$df[1:2]),sum(summary(mod2ALLallies)$df[1:2]))
N <- paste("\\multicolumn{1}{c}{", N, "}", sep = ""); tab <- rbind(tab, N)
addtorow <- list(); addtorow$pos <- list(2,3); addtorow$command <- c(paste0('\\midrule '),paste0(''))
tab1 <- tab

hs6codes <- substr(newdat2_ALL$hs10code, 1, 6)
mods_ALLnonallies <- list(coeftest(mod1ALLnonallies, vcov = vcovCL, cluster = hs6codes)[1:5,1:4], 
  coeftest(mod2ALLnonallies, vcov = vcovCL, cluster = hs6codes)[1:7,1:4])

vars <- c(rownames(mods_ALLnonallies[[1]])[c(5)])
tab <- matrix(data = NA, ncol = length(mods_ALLnonallies), nrow = length(vars)*2)
rownames(tab) <- c(rep(vars, each = 2))
for(i in 1:length(mods_ALLnonallies)){mod <- mods_ALLnonallies[[i]]; tabinmod <- unique(rownames(tab)[rownames(tab) %in% rownames(mod)])
stars <- rep("", length(tabinmod)); stars[mod[tabinmod,4] < .1] <- "^{+}"; stars[mod[tabinmod,4] < .05] <- "^{*}"; 
stars[mod[tabinmod,4] < .01] <- "^{**}"; stars[mod[tabinmod,4] < .001] <- "^{***}";  
tab[rownames(tab) %in% rownames(mod),i] <- c(rbind(paste(myround(mod[tabinmod,1],3), stars, sep = ""), paste("(", myround(mod[tabinmod,2],3), ")", sep = "")))}
rownames(tab) <- c(rbind(vars, "")); rownames(tab)[c(1)] <- 
  c("Post-Trade War$\\cdot$China$\\cdot$Non-allied alternative markets") 
N <- c(sum(summary(mod1ALLnonallies)$df[1:2]),sum(summary(mod2ALLnonallies)$df[1:2]))
N <- paste("\\multicolumn{1}{c}{", N, "}", sep = ""); tab <- rbind(tab, N)
addtorow <- list(); addtorow$pos <- list(2,3); addtorow$command <- c(paste0('\\midrule '),paste0(''))
tab2 <- tab

mrg_rw <- rownames(tab); mrg_rw[1] <-  "Post-Trade War$\\cdot$China$\\cdot$Alternative markets"
tab2_ALL_20212023_allies <- tab1
tab2_ALL_20212023_nonallies <- tab2

ptable(cbind(mrg_rw, tab2_ALL_20182020_allies, tab2_ALL_20212023_allies), "paper/tables/TableA6toptop.tex")
ptable(cbind(mrg_rw, tab2_ALL_20182020_nonallies, tab2_ALL_20212023_nonallies), "paper/tables/TableA6topbot.tex")

###################################################
## Trade models friends and non-friends 20212023 ##
###################################################

## ALL
newdat <- data.frame(year = rep(c("20122016","20212023"), each = 2*nrow(trdmat_excl)), 
        hs10code = trdmat_excl$hs10code,
        imp = c(trdmat_excl$impchn20122016/5, trdmat_excl$impALL20122016/5, trdmat_excl$impchn20212023/3, trdmat_excl$impALL20212023/3)/1000,
        impfriends = c(trdmat_excl$impchn20122016/5, trdmat_excl$impALL20122016friends/5, trdmat_excl$impchn20212023/3, trdmat_excl$impALL20212023friends/3)/1000,
        impnonfriends = c(trdmat_excl$impchn20122016/5, trdmat_excl$impALL20122016nonfriends/5, trdmat_excl$impchn20212023/3, trdmat_excl$impALL20212023nonfriends/3)/1000,
        market = c(rep("China", nrow(trdmat_excl)), rep("Other", nrow(trdmat_excl)), rep("China", nrow(trdmat_excl)), rep("Other", nrow(trdmat_excl))),
        impothpos = c(trdmat_excl$impALLpos20122016, trdmat_excl$impALLpos20122016, trdmat_excl$impALLpos20122016, trdmat_excl$impALLpos20122016),
        impothposfriends = c(trdmat_excl$impALLpos20122016friends, trdmat_excl$impALLpos20122016friends, trdmat_excl$impALLpos20122016friends, trdmat_excl$impALLpos20122016friends),
        impothposnonfriends = c(trdmat_excl$impALLpos20122016nonfriends, trdmat_excl$impALLpos20122016nonfriends, trdmat_excl$impALLpos20122016nonfriends, trdmat_excl$impALLpos20122016nonfriends)
)
newdat$market <- factor(newdat$market, levels = c("Other","China"))

postrade <- trdmat_excl$hs10code[trdmat_excl$impchn20122016/5 > 0 | trdmat_excl$impwor20122016/5 > 0]
newdat2 <- newdat[newdat$hs10code %in% postrade,]
newdat2_ALL <- newdat2
mod <- lm(log(impfriends+1) ~ year*market*log(impothposfriends+1), data = newdat2);
mm_df <- data.frame(model.matrix(mod), impfriends = log(newdat2$impfriends+1), hs10code = newdat2$hs10code)
mm_df <- na.omit(mm_df)
colnames(mm_df) <- c("Intercept","year","market","impothposfriends",
  "year_market","year_impothposfriends","market_impothposfriends","year_market_impothposfriends",
  "impfriends","hs10code")
aggvars <- apply(mm_df[,colnames(mm_df)[2:9]], 2, as.numeric)
mm_df$hs10code_market <- paste(mm_df$hs10code,mm_df$market, sep = "")
agg <- aggregate(aggvars ~ hs10code_market, FUN = mean, data = mm_df)
colnames(agg) <- c("hs10code_market","year_mean","market_mean","impothposfriends_mean",
  "year_market_mean","year_impothposfriends_mean","market_impothposfriends_mean","year_market_impothposfriends_mean",
  "impfriends_mean")
mm_df <- join(mm_df, agg)
mm_df[,2:9] <- mm_df[,2:9] - mm_df[,12:19] 

mod1ALLfriends <- lm(impfriends ~ year + year_market + year_impothposfriends + year_market_impothposfriends, data = mm_df)
mm_df$year_sector <- paste(newdat2$market, newdat2$year, substr(newdat2$hs10code,1,2),sep = "_")
mod2ALLfriends <- lm(impfriends ~ year_impothposfriends + year_market_impothposfriends + year_sector, data= mm_df); 

postrade <- trdmat_excl$hs10code[trdmat_excl$impchn20122016/5 > 0 | trdmat_excl$impwor20122016/5 > 0]
newdat2 <- newdat[newdat$hs10code %in% postrade,]
newdat2_ALL <- newdat2
mod <- lm(log(impnonfriends+1) ~ year*market*log(impothposnonfriends+1), data = newdat2);
mm_df <- data.frame(model.matrix(mod), impnonfriends = log(newdat2$impnonfriends+1), hs10code = newdat2$hs10code)
mm_df <- na.omit(mm_df)
colnames(mm_df) <- c("Intercept","year","market","impothposnonfriends",
  "year_market","year_impothposnonfriends","market_impothposnonfriends","year_market_impothposnonfriends",
  "impnonfriends","hs10code")
aggvars <- apply(mm_df[,colnames(mm_df)[2:9]], 2, as.numeric)
mm_df$hs10code_market <- paste(mm_df$hs10code,mm_df$market, sep = "")
agg <- aggregate(aggvars ~ hs10code_market, FUN = mean, data = mm_df)
colnames(agg) <- c("hs10code_market","year_mean","market_mean","impothposnonfriends_mean",
  "year_market_mean","year_impothposnonfriends_mean","market_impothposnonfriends_mean","year_market_impothposnonfriends_mean",
  "impnonfriends_mean")
mm_df <- join(mm_df, agg)
mm_df[,2:9] <- mm_df[,2:9] - mm_df[,12:19] 

mod1ALLnonfriends <- lm(impnonfriends ~ year + year_market + year_impothposnonfriends + year_market_impothposnonfriends, data = mm_df)
mm_df$year_sector <- paste(newdat2$market, newdat2$year, substr(newdat2$hs10code,1,2),sep = "_")
mod2ALLnonfriends <- lm(impnonfriends ~ year_impothposnonfriends + year_market_impothposnonfriends + year_sector, data= mm_df); 

## write models
hs6codes <- substr(newdat2_ALL$hs10code, 1, 6)
mods_ALLfriends <- list(coeftest(mod1ALLfriends, vcov = vcovCL, cluster = hs6codes)[1:5,1:4], 
  coeftest(mod2ALLfriends, vcov = vcovCL, cluster = hs6codes)[1:7,1:4])

vars <- c(rownames(mods_ALLfriends[[1]])[c(5)])
tab <- matrix(data = NA, ncol = length(mods_ALLfriends), nrow = length(vars)*2)
rownames(tab) <- c(rep(vars, each = 2))
for(i in 1:length(mods_ALLfriends)){mod <- mods_ALLfriends[[i]]; tabinmod <- unique(rownames(tab)[rownames(tab) %in% rownames(mod)])
stars <- rep("", length(tabinmod)); stars[mod[tabinmod,4] < .1] <- "^{+}"; stars[mod[tabinmod,4] < .05] <- "^{*}"; 
stars[mod[tabinmod,4] < .01] <- "^{**}"; stars[mod[tabinmod,4] < .001] <- "^{***}";  
tab[rownames(tab) %in% rownames(mod),i] <- c(rbind(paste(myround(mod[tabinmod,1],3), stars, sep = ""), paste("(", myround(mod[tabinmod,2],3), ")", sep = "")))}
rownames(tab) <- c(rbind(vars, "")); rownames(tab)[c(1)] <- 
  c("Post-Trade War$\\cdot$China$\\cdot$Friendly alternative markets") 
N <- c(sum(summary(mod1ALLfriends)$df[1:2]),sum(summary(mod2ALLfriends)$df[1:2]))
N <- paste("\\multicolumn{1}{c}{", N, "}", sep = ""); tab <- rbind(tab, N)
addtorow <- list(); addtorow$pos <- list(2,3); addtorow$command <- c(paste0('\\midrule '),paste0(''))
tab1 <- tab

hs6codes <- substr(newdat2_ALL$hs10code, 1, 6)
mods_ALLnonfriends <- list(coeftest(mod1ALLnonfriends, vcov = vcovCL, cluster = hs6codes)[1:5,1:4], 
  coeftest(mod2ALLnonfriends, vcov = vcovCL, cluster = hs6codes)[1:7,1:4])

vars <- c(rownames(mods_ALLnonfriends[[1]])[c(5)])
tab <- matrix(data = NA, ncol = length(mods_ALLnonfriends), nrow = length(vars)*2)
rownames(tab) <- c(rep(vars, each = 2))
for(i in 1:length(mods_ALLnonfriends)){mod <- mods_ALLnonfriends[[i]]; tabinmod <- unique(rownames(tab)[rownames(tab) %in% rownames(mod)])
stars <- rep("", length(tabinmod)); stars[mod[tabinmod,4] < .1] <- "^{+}"; stars[mod[tabinmod,4] < .05] <- "^{*}"; 
stars[mod[tabinmod,4] < .01] <- "^{**}"; stars[mod[tabinmod,4] < .001] <- "^{***}";  
tab[rownames(tab) %in% rownames(mod),i] <- c(rbind(paste(myround(mod[tabinmod,1],3), stars, sep = ""), paste("(", myround(mod[tabinmod,2],3), ")", sep = "")))}
rownames(tab) <- c(rbind(vars, "")); rownames(tab)[c(1)] <- 
  c("Post-Trade War$\\cdot$China$\\cdot$Non-friendly alternative markets") 
N <- c(sum(summary(mod1ALLnonfriends)$df[1:2]),sum(summary(mod2ALLnonfriends)$df[1:2]))
N <- paste("\\multicolumn{1}{c}{", N, "}", sep = ""); tab <- rbind(tab, N)
addtorow <- list(); addtorow$pos <- list(2,3); addtorow$command <- c(paste0('\\midrule '),paste0(''))
tab2 <- tab

mrg_rw <- rownames(tab); mrg_rw[1] <-  "Post-Trade War$\\cdot$China$\\cdot$Alternative markets"
tab2_ALL_20212023_friends <- tab1
tab2_ALL_20212023_nonfriends <- tab2

ptable(cbind(mrg_rw, tab2_ALL_20182020_friends, tab2_ALL_20212023_friends), "paper/tables/TableA6bottop.tex")
ptable(cbind(mrg_rw, tab2_ALL_20182020_nonfriends, tab2_ALL_20212023_nonfriends), "paper/tables/TableA6botbot.tex")


